<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="utf-8" />
	<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
	<meta name="viewport" content="width=device-width, initial-scale=1" />
	<meta name="theme-color" content="#000000" />
	<meta name="description" content="Web site created using create-react-app" />
	<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
	<!--
      manifest.json provides metadata used when your web app is installed on a
      user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
    -->
	<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />

	<!--
      Notice the use of %PUBLIC_URL% in the tags above.
      It will be replaced with the URL of the `public` folder during the build.
      Only files inside the `public` folder can be referenced from the HTML.

      Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
      work correctly both with client-side routing and a non-root public URL.
      Learn how to configure a non-root public URL by running `npm run build`.
    -->
	<title>React App</title>
	<link href="css/jquery-ui.css" rel="stylesheet">
	<link href="css/mui.css" rel="stylesheet">
	<script src="%PUBLIC_URL%/react.production.min.js"></script>
	<script src="js/jquery.js"></script>
	<script src="js/jquery-ui.js"></script>
	<link rel="stylesheet" href="css/font-awesome.min.css">


	<style>
		#guiroot {
			position: absolute;
			top: 0px;
			left: 0px;
			width: 100%;
			height: 100%;

		}

		.MyCenterPanel {
			position: absolute;
			top: 0px;

			box-shadow: 10px 5px 5px grey;
			margin-top: 0;
			margin-right: auto;
			margin-bottom: 0;
			margin-left: auto;
			background-color: greenyellow;
		}
	</style>
</head>

<body>
	<script>

		// 1. 一个简单的 Controller ，只会出现在浏览器的中心，可以控制宽度和高度。显示标题，可以关闭。


		console.log("in global domain.");

		//step 1 定义全局ui对象
		const ui = {};
		ui.root = {};
		ui.tools = {};
		// string of width of height add some number and return string of new width or height with 'px'
		ui.tools.sizePxAdd = function (pxstr, nval) {
			let val = parseInt(pxstr.substr(0, pxstr.length - 2)) + nval;
			return val + 'px';
		}
		ui.tools.valueOfPx = function (pxstr) {
			return parseInt(pxstr.substr(0, pxstr.length - 2));
		}
		console.log(ui);


		//step 2 浏览器全局对象window定义ui对象并绑定全球ui对象，这样可以在别的widget文件中获取ui全局对象
		window.ui = ui;

		ui.Button = function (title, iWid, iHei, callback) {
			var element = $(document.createElement("div")).button({ label: title });
			element.css("width", iWid+'px') ;
			element.css("height",iHei+'px') ;
			element.on('click', callback);
			return element;
		}

		ui.Label = function (title) {
			var element = $(document.createElement("div"));
			element.html(title);
			return element;
		}

		ui.Imager = function( url , iWid, iHei ){
			var element = $(document.createElement("img"));
			element.attr('width', iWid + 'px');
			element.attr('height',iHei + 'px');
			element.attr('src', url);
			return element;
		}




		console.log("end global domain.");

		//2023-5-11 -------------------------------------------------------
		ui.Panel = function (title, iWid, iHei , tcss) {
			var d1 = $(document.createElement("div"));
			let css = { ...tcss, 
				width: iWid+'px' ,
				height:iHei+'px' ,
				position: 'absolute'
				, left: '0px'
				, top: '0px' 
 				//flexbox Important
				, display:'flex'
				, 'flex-direction':'row'
				, 'flex-wrap':'wrap'
				,'justify-content':'center'
				,'align-items': 'center'
			};

			if (typeof css["background-color"] === 'undefined') {
				css["background-color"] = "#FFF";
			}
			d1.css(css);
			d1.title = title;
			d1.add = function (w) {
				this.append(w);
			}
			d1.width =  iWid;
			d1.height = iHei;
			return d1;
		}


		ui.root.add = function (panel) {
			$("#guiroot").append(panel);
		}




		//StackController构造函数
		ui.StackController = function ( iWid, iHei , tcss, rootPanel, showClose = true) {
			const isrcWidth = $(window).width() ;
			const iscrHeight = $(window).height() ;
			const ileft = isrcWidth/2 - iWid/2 ;
			const itop = iscrHeight/2 - iHei/2 - 44;

			var d1 = $(document.createElement("div"));
			let css = {
				...tcss,
				position: 'absolute',
				left: ileft+'px',
				top:  itop+'px' ,
				"box-shadow": "rgba(99, 99, 99, 0.2) 0px 2px 8px 0px"
			};
			d1.css(css);

			var header = $(document.createElement("div"));
			header.css({
				"position": "absolute", 
				"left": "0px", 
				"top": "0px", "width": "100%", 
				"height": "44px", 
				"background-color": "#FFF", "line-height": "44px",
				"box-shadow": "rgba(0, 0, 0, 0.1) 0px 1px 0px 0px"
			});
			var titleLabel = $(document.createElement("span"));
			titleLabel.css({ "display": "inline-block", "width": "100%", "text-align": "center" })
			header.append(titleLabel);

			//后退按钮
			var backButton = $(document.createElement("button"));
			backButton.html("<i class='fa fa-angle-left fa-2x'></i>");
			backButton.hide();
			backButton.addClass("mui-btn mui-btn--flat mui-btn--primary");
			backButton.css({
				"position": "absolute", "left": "0px", "top": "0px", "margin": "0px", "height": "44px",
				"padding-left": "10px", "padding-right": "10px"
			});
			d1.backButton = backButton;
			header.append(backButton);

			//关闭按钮
			var closeButton = $(document.createElement("button"));
			closeButton.html("<i class='fa fa-remove'></i>");
			if (showClose === false) closeButton.hide();
			closeButton.addClass("mui-btn mui-btn--flat mui-btn--primary");
			closeButton.css({
				"position": "absolute", "right": "0px", "top": "0px", "margin": "0px", "height": "44px",
				"padding-left": "10px", "padding-right": "10px"
			});
			d1.closeButton = closeButton;
			header.append(closeButton);

			d1.titleLabel = titleLabel;

			let containerHeight = ( iHei - 44) + 'px';

			var container = $(document.createElement("div"));
			container.css({
				"position": "absolute",
				"left": "0px",
				"top": "44px",
				"width": "100%",
				"height": containerHeight,
				"background-color": "#FFF",
				"overflow": "auto"
			});

			d1.append(container);
			d1.append(header);

			d1.container = container;
			d1.header = header;

			//PUSH 函数定义
			d1.push = function (panel, useAnim = true) {
				if (useAnim === true) {
					panel.toggle("slide");
				}
				this.panelList.push(panel);
				this.container.append(panel);
				this.titleLabel.text(panel.title);
				if (this.panelList.length > 1) {
					this.backButton.show();
					//hide lower one
					const oldPanel = this.panelList[ this.panelList.length-2 ] ;
					oldPanel.hide() ;
				}
				//resize container by panel
				this.container.css("width",  panel.width+'px');
				this.container.css("height", panel.height+'px');
				const newileft = ($(window).width() - panel.width)/2;
				const newitop = ($(window).height() -panel.height)/2-44 ;
				console.log('newileft '+ newileft);
				if (useAnim === true) {
					//resize controller by panel 
					this.animate({
						left:newileft+'px',
						top:newitop+'px',
						width: panel.width+'px',
						height: (panel.height+44)+'px' 
					}, 500);
				} else {
					//no animation
					this.css("left", newileft+'px');
					this.css("top",  newitop+'px');
					this.css("width", panel.width+'px');
					this.css("height", (panel.height+44)+'px' );
				}
			}
			//POP 函数定义
			d1.pop = function () {
				if (this.panelList.length > 1) {
					let popOne = this.panelList.pop();
					//old one give away.
					popOne.toggle("slide", function () { popOne.remove(); });
					if (this.panelList.length >= 1) {
						const newLastPanel = this.panelList[this.panelList.length - 1];
						//show newPanel below
						newLastPanel.show() ;

						this.titleLabel.text(newLastPanel.title);
						//resize container by panel
						this.container.css("width", newLastPanel.width+'px');
						this.container.css("height", newLastPanel.height+'px');
						const newileft = ($(window).width() - newLastPanel.width)/2;
						const newitop = ($(window).height() - newLastPanel.height)/2-44 ;
						//animate
						this.animate({
							left: newileft+'px',
							top:  newitop +'px',
							width: newLastPanel.width+'px',
							height: (newLastPanel.height+44)+'px'
						}, 500);
					}
					if (this.panelList.length <= 1) {
						this.backButton.hide();
					}
				}
			}
			d1.close = function () {
				d1.panelList = null;
				d1.remove();
			}

			d1.backButton.click(function () { d1.pop(); });
			d1.closeButton.click(function () { d1.close(); });
			d1.panelList = [];
			d1.push(rootPanel, false);

			return d1;
		}




	</script>
	<noscript>You need to enable JavaScript to run this app.</noscript>

	<div id="guiroot"></div>
	<div id="root"></div>
	<div style="position:absolute;bottom:0px;">
		<button onclick="onSlideClick()">slide</button>

		<button onclick="onTestSimpleController()">Test SimpleController</button>
	</div>
	<!--
      This HTML file is a template.
      If you open it directly in the browser, you will see an empty page.

      You can add webfonts, meta tags, or analytics to this file.
      The build step will place the bundled scripts into the <body> tag.

      To begin the development, run `npm start` or `yarn start`.
      To create a production bundle, use `npm run build` or `yarn build`.
    -->

	<script>

		onTestSimpleController = function () {
			let stack = {};

			let panel1 = ui.Panel('Panel1', 800 , 300 , {});
			panel1.add(ui.Button('push panel2', 300, 40, function () {
				let panel2 = ui.Panel('Panel2', 800 , 600 , {});
				panel2.add(ui.Label("This is Panel2."));
				panel2.add(ui.Button('push PANEL-III', 800, 60 , function() {
					const panel3 = ui.Panel('Panel-III',200,200,{}) ;
					stack.push(panel3) ;
				})) ;

				for(var i = 0 ; i<30 ; ++ i ){
					const img1 = ui.Imager('images/placeholder1.png','200','80') ;
					img1.css('margin','5px') ;
					panel2.add(img1) ;
				}


				stack.push(panel2);
			}));
			panel1.add(ui.Label('label1'));
			//ui.root.add(panel1) ;

			stack = ui.StackController( 800 ,  300 , {}, panel1);
			ui.root.add(stack);

		}


	</script>
</body>

</html>